package greate.projcet;

import org.apache.camel.AggregationStrategy;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.builder.AggregationStrategies;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author v_zhangshun01
 */
public class App {
    static List<String> list = new ArrayList<>();

    public static void main(String[] args) throws Exception {

        // 创建默认上下文
        CamelContext context = new DefaultCamelContext();

        // 现开启想表达的是 ， camel 在开启后可以动态的新增和关闭路由。
        context.start();

        // 新增路由
        context.addRoutes(new HelloWorldRoute());

        Thread.sleep(10000);

        // 关闭
        context.close();

    }

    public static class HelloWorldRoute extends RouteBuilder {
        @Override
        public void configure() throws Exception {

            List<String> list = new ArrayList<>();
            for (int i = 0; i < 200000; i++) {
                list.add("{\n" +
                        "    \"dataTimeDt\": \"2021-07-05 07:15:00\",\n" +
                        "    \"dataTimeLong\": 1625440500000,\n" +
                        "    \"dataTimeStr\": \"20210705071500\",\n" +
                        "    \"enterpriseCode\": \"GY-2-0675\",\n" +
                        "    \"orgMeterCode\": \"00001119600000001003\",\n" +
                        "    \"paraCode\": \"450001\",\n" +
                        "    \"value\": 0.0000\n" +
                        "  }");
            }
             MyBodyAppender appender = new MyBodyAppender();


            from("timer://foo?repeatCount=1")
                    .setBody(constant(list))
                    .split(body())
//                    .log("1")
                    .aggregate(constant(true), AggregationStrategies.bean(appender, "append"))
//                    .completionSize(2)
                    .completionInterval(3000)
                    .process(exchange -> list.clear())
                    .log("${body.size()}");

        }
    }

    public static class MyBodyAppender {


        public List<String> append(String existing, String next) throws InterruptedException {
            list.add(next);
            return list;
        }

    }

}
